package com.AnywheeBt;

import java.util.Properties;

import com.AnywheeBt.PbapClient.utils.StringUtils;

import android.app.Application;
import android.content.Context;
import android.util.Log;

/*
 * BTApp 全局变量保存
 */
public class BTApp extends Application {
	private boolean mbAutoConnect = true;
	private boolean mbAutoAnswer = true;
	private boolean bDDH = true;
	private String mBtName = "CAR-KIT";
	private String mPinNum = "0000";
	private static BTApp mInstance = null;
	public final static String AUTO_CONNECT_STR = "autoconnect";
	public final static String AUTO_ANSWER_STR = "autoanswer";
	public final static String DDH_STR = "ddh";
	public final static String BT_NAME = "btname";
	public final static String BT_PIN = "btpin";
	
	public static BTApp getAppConext(Context context) {
		if (mInstance == null) {
			mInstance = new BTApp();
		}
		return mInstance;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mInstance = this;
		Init();
	}
	
	/*
	 * Init
	 */
	public void Init(){
		mbAutoConnect = isMbAutoConnect();
		mbAutoAnswer = isMbAutoAnswer();
		if(!mbAutoConnect){
			
		}
	}
		
	public String getmBtName() {
		String btName = getProperty(BT_NAME);
		return btName;
	}

	public void setmBtName(String btName) {
		setProperty(BT_NAME, btName);
		this.mBtName = btName;
	}

	public String getmPinNum() {
		String btPin = getProperty(BT_PIN);
		if(btPin == null){
			btPin = "0000";
			setProperty(BT_PIN, btPin);
		}
		return btPin;
	}

	public void setmPinNum(String pinNum) {
		setProperty(BT_PIN, pinNum);
		this.mPinNum = pinNum;
	}

	public boolean isDDH() {
		String ecar = getProperty(DDH_STR);
		//默认
		if(StringUtils.isEmpty(ecar)){
			setDDH(true);
			return true;
		}
		else{
			return StringUtils.toBool(ecar);
		}
	}

	public void setDDH(boolean flag) {
		setProperty(DDH_STR, String.valueOf(flag));
		this.bDDH = flag;
	}
	
	public boolean isMbAutoConnect() {
		String autoConnect = getProperty(AUTO_CONNECT_STR);
		//默认
		if(StringUtils.isEmpty(autoConnect)){
			setMbAutoConnect(true);
			return true;
		}
		else{
			return StringUtils.toBool(autoConnect);
		}
	}

	public void setMbAutoConnect(boolean mbAutoConnect) {
		setProperty(AUTO_ANSWER_STR, String.valueOf(mbAutoConnect));
	}

	public boolean isMbAutoAnswer() {
		String autoAnswer = getProperty(AUTO_ANSWER_STR);
		Log.e("Yanglin", "autoAnswer = " + autoAnswer);
		//默认
		if(StringUtils.isEmpty(autoAnswer)){
			setMbAutoAnswer(true);
			return true;
		}
		else{
			return StringUtils.toBool(autoAnswer);
		}
	}
	
	public void setMbAutoAnswer(boolean mbAutoAnswer) {
		setProperty(AUTO_ANSWER_STR, String.valueOf(mbAutoAnswer));
	}

	public boolean containsProperty(String key){
		Properties props = getProperties();
		 return props.containsKey(key);
	}
	
	public void setProperties(Properties ps){
		AppConfig.getAppConfig(this).set(ps);
	}

	public Properties getProperties(){
		return AppConfig.getAppConfig(this).get();
	}
	
	public void setProperty(String key, String value){
		AppConfig.getAppConfig(this).set(key, value);
	}
	
	public String getProperty(String key){
		return AppConfig.getAppConfig(this).get(key);
	}
	public void removeProperty(String...key){
		AppConfig.getAppConfig(this).remove(key);
	}
}
